package com.grace.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.grace.common.RequestHolder;
import com.grace.dao.SysRoleAclMapper;
import com.grace.dao.SysRoleMapper;
import com.grace.dao.SysRoleUserMapper;
import com.grace.dao.SysUserMapper;
import com.grace.exception.ParamException;
import com.grace.model.SysRole;
import com.grace.model.SysUser;
import com.grace.param.RoleParam;
import com.grace.util.BeanValidator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysLogService sysLogService;

    /**
     * 新增操作
     */
    public void save(RoleParam param) {
        //校验对象
        BeanValidator.check(param);
        if (checkExist(param.getName(), param.getId())) {
            throw new ParamException("已存在相同的角色名称");
        }
        //构建对象
        SysRole sysRole = SysRole.builder().name(param.getName()).type(param.getType())
                .status(param.getStatus()).remark(param.getRemark()).build();
        sysRole.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysRole.setOperatorTime(new Date());
        //插入数据库
        sysRoleMapper.insertSelective(sysRole);

        //记录日志
        sysLogService.saveRoleLog(null, sysRole);

    }

    /**
     * 更新操作
     */
    public void update(RoleParam param) {
        //校验对象
        BeanValidator.check(param);
        if (checkExist(param.getName(), param.getId())) {
            throw new ParamException("已存在相同的角色名称");
        }
        //根据id校验当前对象是否存在
        SysRole before = sysRoleMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的角色不存在");
        //构建对象
        SysRole after = SysRole.builder().name(param.getName()).type(param.getType())
                .status(param.getStatus()).remark(param.getRemark()).build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperatorTime(new Date());
        sysRoleMapper.updateByPrimaryKeySelective(after);

        //记录日志
        sysLogService.saveRoleLog(before, after);
    }

    /**
     * 判断是否角色名已经存在
     */
    public boolean checkExist(String name, Integer id) {
        return sysRoleMapper.countByName(name, id) > 0;
    }

    /**
     * 获取角色列表
     */
    public List<SysRole> getAll() {
        return sysRoleMapper.findAll();
    }

    /**
     * 根据userid获取角色列表
     */
    public List<SysRole> getRoleListByUserId(int userId) {
        //根据userId获取角色列表
        List<Integer> roleIdList = sysRoleUserMapper.getRoleIdListByUserId(userId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        //根据roleid获取角色列表
        return sysRoleMapper.getByIdList(roleIdList);
    }


    /**
     * 根据AclId得到角色列表
     *
     * @param aclId
     * @return
     */
    public List<SysRole> getRoleListByAclId(int aclId) {
        List<Integer> roleIdList = sysRoleAclMapper.getRoleIdListByAclId(aclId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        List<SysRole> roles = sysRoleMapper.getByIdList(roleIdList);
        return roles;
    }

    /**
     * 根据RoleIdList得到UserList
     *
     * @param roles
     * @return
     */
    public List<SysUser> getUserListByRoleList(List<SysRole> roles) {
        if (CollectionUtils.isEmpty(roles)) {
            return Lists.newArrayList();
        }
        //通过RoleList来获取用户Id的列表
        //使用流式得到roleIdList
        List<Integer> roleIdList = roles.stream().map(role -> role.getId()).collect(Collectors.toList());
        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleIdList(roleIdList);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        List<SysUser> userList = sysUserMapper.getUserListByIdList(userIdList);
        if (CollectionUtils.isEmpty(userList)) {
            return Lists.newArrayList();
        }
        return userList;


    }
}
